import { login, getUserInfo } from '@/api/login'
// import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    avatar: '',
    wxid: '',
    email: '',
    phone: '',
    gender: '',
    birth: '',
    balance: 0,
    club: 0,
    activity: 0,
    ispr: '',
    user: {},
    name: '',
    en_name: '',
    ready_email: '',
    wechat: '',
    // 以下專屬會員配置
    area: '',
    id_number: '',
    trade: '',
    is_voter: '',
    address: '',
    company: '',
    vocation: '',
    interest: '',
    speciality: '',
    education: '',
    social_vocation: '',
    referrer: '',
    hk_area: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_EN_NAME: (state, enName) => {
      state.en_name = enName
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_WXID: (state, wxid) => {
      state.wxid = wxid
    },
    SET_EMAIL: (state, email) => {
      state.email = email
    },
    SET_PHONE: (state, phone) => {
      state.phone = phone
    },
    SET_GENDER: (state, gender) => {
      // state.gender = gender === 0 ? '未知': gender === 1 ? '男' : '女'
      state.gender = gender
    },
    SET_BIRTH: (state, birth) => {
      state.birth = birth
    },
    SET_ACTIVITY: (state, activity) => {
      state.activity = activity
    },
    SET_BALANCE: (state, balance) => {
      state.balance = balance
    },
    SET_CLUB: (state, club) => {
      state.club = club
    },
    SET_ISPR: (state, ispr) => {
      state.ispr = ispr
    },
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_READY_EMAIL: (state, readyEmail) => {
      state.ready_email = readyEmail
    },
    SET_WECHAT: (state, wechat) => {
      state.wechat = wechat
    },
    SET_AREA: (state, area) => {
      state.area = area
    },
    SET_ID_NUMBER: (state, idNumber) => {
      state.id_number = idNumber
    },
    SET_TRADE: (state, trade) => {
      state.trade = trade
    },
    SET_IS_VOTER: (state, isVoter) => {
      state.is_voter = isVoter
    },
    SET_ADDRESS: (state, address) => {
      state.address = address
    },
    SET_COMPANY: (state, company) => {
      state.company = company
    },
    SET_VOCATION: (state, vocation) => {
      state.vocation = vocation
    },
    SET_INTEREST: (state, interest) => {
      state.interest = interest
    },
    SET_SPECIALITY: (state, specify) => {
      state.speciality = specify
    },
    SET_EDUCATION: (state, education) => {
      state.education = education
    },
    SET_SOCIAL_VOCATION: (state, socialVocation) => {
      state.social_vocation = socialVocation
    },
    SET_REFERRER: (state, referrer) => {
      state.referrer = referrer
    },
    SET_HK_AREA: (state, hkArea) => {
      state.hk_area = hkArea
    }
  },

  actions: {
    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetUserInfo ({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo().then(response => {
          const data = response.data
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_EMAIL', data.email)
          commit('SET_WXID', data.wxid)
          commit('SET_EN_NAME', data.en_name)
          commit('SET_PHONE', data.phone)
          commit('SET_GENDER', data.gender)
          commit('SET_BIRTH', data.birth)
          commit('SET_ACTIVITY', data.activity)
          commit('SET_BALANCE', data.balance)
          commit('SET_CLUB', data.club)
          commit('SET_ISPR', data.ispr)
          commit('SET_USER', data)
          commit('SET_READY_EMAIL', data.ready_email)
          commit('SET_WECHAT', data.wechat)
          commit('SET_AREA', data.area)
          commit('SET_ID_NUMBER', data.id_number)
          commit('SET_TRADE', data.trade)
          commit('SET_IS_VOTER', data.is_voter)
          commit('SET_ADDRESS', data.address)
          commit('SET_COMPANY', data.company)
          commit('SET_VOCATION', data.vocation)
          commit('SET_INTEREST', data.interest)
          commit('SET_SPECIALITY', data.speciality)
          commit('SET_EDUCATION', data.education)
          commit('SET_SOCIAL_VOCATION', data.social_vocation)
          commit('SET_REFERRER', data.referrer)
          commit('SET_HK_AREA', data.hk_area)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }

    // // 登出
    // LogOut({ commit, state }) {
    //   return new Promise((resolve, reject) => {
    //     logout(state.token).then(() => {
    //       commit('SET_TOKEN', '')
    //       commit('SET_ROLES', [])
    //       removeToken()
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },
    //
    // // 前端 登出
    // FedLogOut({ commit }) {
    //   return new Promise(resolve => {
    //     commit('SET_TOKEN', '')
    //     removeToken()
    //     resolve()
    //   })
    // }
  }
}

export default user
